<?php
/************************************
*   新浪相册 7.0
*   @file:controllor.php
*   @intro:控制器基类
*   @author:shaopu@
*   @date:Sun Sep 20 17:34:03 CST 2009
************************************/

class controllor 
{
    public $visitor    = array();             //访问者数据
    public $master     = array();             //被访问者数据
    
    protected $login      = false;               //是否登录相册
    public    $myself = false;                   //是否自己访问自己
    protected $__need_login = true;
    protected $__need_session = true;
    private   $__template_name = '';             //模板名称
    protected $__start_time;                     //程序开始运行时间
    protected $__start_memory;                   //程序开始使用的内存
    protected $__get_param = array();
    
    /**
     * 构造函数
     * 
     *
     * @return controllor
     */
	protected function controllor($get_param)
	{
	    $this->__get_param = $get_param;
	    $this->__start_time = K::microtime();
	    $this->__start_memory = memory_get_usage();
	}
	
	/**
	 * 初始化SESSION
	 * 检测COOKIE判断当前用户是否登录
	 *
	 * @return true
	 */
	protected function init_session()
	{
	    $oSess = K::session();
	    $oSess->get(K_SESSION_KEY_LOGIN , $login_info);

	    //非登录用户
	    if(!is_uid($login_info['uid']))
            return true;
            
	    //以下是登录用户
	    $oMember = K::load_mod('member');
	    $oMember->get_user($login_info['uid']) or $this->busy();
	    $login_info = $oMember->get_data();
	    $this->login = true;
	    $this->visitor = $login_info;
	    
	    return true;
	}

	

	/**
	 * get the input
	 * caution!! could not get the array like this : ?a[]=1&a[]=2&a[]=3
	 *
	 * @param string $key
	 * @param string $method  G/GET P/POST OTHER
	 * @param mix $default	//default value when !isset
	 * @return mix
	 */
	protected function input($key , $method = 'all' , $default = null)
	{
		$method = strtoupper($method);
		if('G' == $method || 'GET' == $method)
        {
            $value = isset($_GET[$key]) ? $_GET[$key] : $default;
            $this->__input[$key] = trim($value);
        }
        if('P' == $method || 'POST' == $method)
        {
            $value = isset($_POST[$key]) ? $_POST[$key] : $default;
            $this->__input[$key] = trim($value);
        }
        else
        {
            $value = isset($_GET[$key]) 
                    ? $_GET[$key] 
                    : (isset($_POST[$key]) 
                        ? $_POST[$key] 
                        : $default);
            $this->__input[$key] = trim($value);
        }
        
        return trim($value);
	}
    protected function get_input($k)
    {
        return $this->__get_param[$k];
    }
    
	
	
	protected function _over()
	{
	    //记录程序运行总时间
	    $time = K::microtime() - $this->__start_time;
	    $over_memory = memory_get_usage() - $this->__start_memory;
	    $peak_memory = memory_get_peak_usage() - $this->__start_memory;
	    
	    K_log::debug('benchmark' , 'run_time:'.$time.TAB.'over_memory:'.$over_memory.TAB.'max_memory:'.$peak_memory);
	}
	
	//系统繁忙，需要在子类中被覆盖
	protected function busy()
	{
	    
	}
	/**
     * 私有初始化，会在RUN之前执行，各程序根据自己需要进行覆盖
     *
     */
    public function construct(){}
    /**
     * 页面重定向
     *
     * @param string $url
     * @param int $time     //
     */
    protected function redirect($url , $time = 0)
    {
        $url = str_replace(array("\n", "\r"), '', $url);
		if (!headers_sent())
		{
			header("Content-Type:text/html; charset=utf-8");
			if (0 === $time)
			{
				header("Location: ".$url); 
			}
			else 
			{
				header("Refresh:{$time};url={$url}");
			}
			exit;
		}
		else
		{
		    $this->_header_no_cache();
			$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
			exit($str);
		}
    }
        /**
     * 发送禁止浏览器CACHE的HTTP头信息
     *
     */
    protected function _header_no_cache()
    {
        if (!headers_sent())
		{
			header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Data in the past 
			header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); //Modified 
			header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1 
			header('Cache-Control: post-check=0, pre-check=0', false);
			header('Pragma: no-cache'); // HTTP/1.0
		}
		return true;
    }
}
?>